package com.ctshk.rpc.tour.mapper.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.tour.dto.schedule.TripScheduleDTO;
import com.ctshk.rpc.tour.req.schedule.ScheduleDepartureListQueryReq;
import com.ctshk.rpc.tour.req.schedule.TripScheduleListQueryReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author Lenovo
 */
public class SeriesTripScheduleDepartureProvider {

    public String scheduleDepartureList(ScheduleDepartureListQueryReq scheduleDepartureListQueryReq) {
        return new SQL() {{
            SELECT("sts.id,sts.sort_key,sts.schedule_status,st.title,st.trip_code,sts.departure_date,st.travel_days,st.night_days,ststr.max_people,ststr.min_people,ststr.reserved_seats_num,sts.trip_number,sts.trip_id, sts.collection_place_json,sts.dissolution_area_json,sts.departure_consulation,sts.departure_time_type,sts.departure_time,sts.departure_traffic_type_id,sts.departure_traffic_type,sts.return_consulation,sts.return_time,sts.return_arrive_date_type,sts.return_arrive_time,sts.return_traffic_type_id,sts.return_traffic_type\n" +
                    ",ststr.has_tea_party,ststr.tea_party_loc_id,ststr.tea_party_date,ststr.tea_party_time,ststr.tea_party_remark,sts.cost_sharing_type,sts.back_date,\n" +
                    "st.departure_place_id,st.departure_place,st.destination_id,st.destination,sts.departure_consulation,sts.departure_time_type,sts.departure_time,sts.go_arrive_date_type,sts.go_arrive_time,sts.departure_traffic_type_id,\n" +
                    "sts.departure_traffic_type,sts.return_consulation,sts.return_time,sts.return_arrive_date_type,sts.return_arrive_time,sts.return_traffic_type_id,sts.return_traffic_type,ststr.enable_additional_position,ststr.clear_seats_time,ststr.clear_seats_unit");
            FROM("t_series_trip_schedule sts");
            LEFT_OUTER_JOIN("t_series_trip st ON ( sts.trip_id = st.id )");
            LEFT_OUTER_JOIN("t_series_trip_schedule_team_rule ststr ON (sts.id = ststr.schedule_id)");

            if (null != scheduleDepartureListQueryReq.getDepartureDate()) {
                WHERE("sts.departure_date = #{scheduleDepartureListQueryReq.departureDate}");
            }
            if (StringUtils.isNotBlank(scheduleDepartureListQueryReq.getDate())){
                WHERE("DATE_FORMAT(sts.departure_date,'%Y-%m') = #{scheduleDepartureListQueryReq.date}");
            }

            if (null!= scheduleDepartureListQueryReq.getTripId()){
                WHERE("sts.trip_id = #{scheduleDepartureListQueryReq.tripId}");
            }

            if (StringUtils.isNotBlank(scheduleDepartureListQueryReq.getSortKey())){
                WHERE("sts.sort_key = #{scheduleDepartureListQueryReq.sortKey}");
            }

            ORDER_BY("sts.gmt_create desc");
        }}.toString();
    }
}
